/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.graph;

import com.jme3.math.Vector3f;
import technotheist.geo.map.MapUtil;

/**
 *
 * @author Mitch
 */
public class GeoCell extends GeoNode<GeoCell, GeoTriangle> {
    public final int index;
    final GeoTriangle[] v;
    final HalfEdge<GeoTriangle, GeoCell>[] b;
    final HalfEdge<GeoCell, GeoTriangle>[] n;

    public GeoCell(int index, final GeoGrid outer) {
        super(outer.cellPos.get(index, new Vector3f()), outer);
//        this.outer = outer;
        this.index = index;
        if (outer.cellPos.isPent(index)) {
            v = new GeoTriangle[MapUtil.PENT];
            b = new HalfEdge[MapUtil.PENT];
            n = new HalfEdge[MapUtil.PENT];
        } else {
            v = new GeoTriangle[MapUtil.HEX];
            b = new HalfEdge[MapUtil.HEX];
            n = new HalfEdge[MapUtil.HEX];
        }
    }
    
    public void setTriangles(GeoTriangle... tris) {
        for(int i = 0; i < v.length; i++) {
            v[i] = tris[i];
            for(int j = 0; j < 3; j++) {
                if(v[i].getBoundEdge(j).origin == this) {
                    n[i] = v[i].getBoundEdge(j);
                }
            }
        }
        for(int i = 0; i < v.length; i++) {
            b[i] = new HalfEdge<GeoTriangle, GeoCell>(v[i], v[(i+1)%sideCount()], this);
            b[i].initSymm(grid.cellEdges);
        }
    }

    public boolean isHex() {
        return v.length == MapUtil.HEX;
    }

    @Override
    public int ID() {
        return index;
    }

    @Override
    public int sideCount() {
        return b.length;
    }

    @Override
    public HalfEdge<GeoTriangle, GeoCell> getBoundEdge(int i) {
        if (i < 0 || i >= sideCount()) {
            throw new IllegalArgumentException("Bound index must be between 0 and " + sideCount() + ".");
        }
        return b[i];
    }

    @Override
    public HalfEdge<GeoCell, GeoTriangle> getNextEdge(int i) {
        if (i < 0 || i >= sideCount()) {
            throw new IllegalArgumentException("Next index must be between 0 and " + sideCount() + ".");
        }
        return n[i];
    }

    
}
